package com.ghp.admin.service.impl;


import cn.hutool.core.util.BooleanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ghp.admin.mapper.InterfaceInfoMapper;
import com.ghp.admin.service.InterfaceInfoService;
import com.ghp.admin.service.UserInterfaceInfoService;
import com.ghp.admin.service.UserService;
import com.ghp.admin.utils.CacheClient;
import com.ghp.common.model.dto.DeleteIdDTO;
import com.ghp.common.model.dto.QueryIdDTO;
import com.ghp.common.model.dto.UpdateIdDTO;
import com.ghp.common.model.dto.interfaceinfo.InterfaceInfoAddDTO;
import com.ghp.common.model.dto.interfaceinfo.InterfaceInfoInvokeDTO;
import com.ghp.common.model.dto.interfaceinfo.InterfaceInfoQueryDTO;
import com.ghp.common.model.dto.interfaceinfo.InterfaceInfoUpdateDTO;
import com.ghp.common.model.entity.InterfaceInfo;
import com.ghp.common.model.entity.User;
import com.ghp.common.model.entity.UserInterfaceInfo;
import com.ghp.common.model.enums.InterfaceInfoEnum;
import com.ghp.common.model.vo.InterfaceInfoVO;
import com.ghp.common.model.vo.RequestParamsRemarkVO;
import com.ghp.common.model.vo.ResponseParamsRemarkVO;
import com.ghp.common.model.vo.UserVO;
import com.ghp.common.utils.BeanConvertorUtils;
import com.ghp.common.utils.LambdaQueryWrapperUtils;
import com.ghp.common.utils.ThreadLocalUtils;
import com.ghp.common.utils.ThrowUtils;
import com.ghp.common.utils.response.ResponseResult;
import com.ghp.feign.client.ApiClient;
import com.google.gson.Gson;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import static com.ghp.common.constants.ErrorMessageConstants.*;
import static com.ghp.common.constants.RedisConstants.*;
import static com.ghp.common.model.enums.InterfaceInfoEnum.DEFAULT_LEFTNUM;
import static com.ghp.common.utils.response.ResponseStatus.*;

/**
 * @author ghp
 * @description 针对表【tb_interface_info(接口信息表)】的数据库操作Service实现
 * @createDate
 */
@Service
public class InterfaceInfoServiceImpl extends ServiceImpl<InterfaceInfoMapper, InterfaceInfo>
        implements InterfaceInfoService {

    @Resource
    private UserInterfaceInfoService userInterfaceInfoService;

    @Resource
    private UserService userService;

    @Resource
    private CacheClient cacheClient;

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 新增接口
     *
     * @param interfaceInfoAddDTO
     * @return
     */
    @Override
    public ResponseResult addInterfaceInfo(InterfaceInfoAddDTO interfaceInfoAddDTO) {
        // 补充接口信息，然后新增
        InterfaceInfo interfaceInfo = BeanConvertorUtils.copyBean(interfaceInfoAddDTO, InterfaceInfo.class);
        interfaceInfo.setUserId(ThreadLocalUtils.getCurrentId());
        interfaceInfo.setRequestParamsRemark(JSONUtil.toJsonStr(interfaceInfoAddDTO.getRequestParamsRemark()));
        interfaceInfo.setResponseParamsRemark(JSONUtil.toJsonStr(interfaceInfoAddDTO.getResponseParamsRemark()));
        boolean isSuccess = this.save(interfaceInfo);
        ThrowUtils.throwIf(!isSuccess, SERVICE_INSERT_ERROR);
        // 新增接口剩余调用次数

        return ResponseResult.success();
    }

    /**
     * 根据id删除接口
     *
     * @param deleteIdDTO
     * @return
     */
    @Override
    public ResponseResult deleteInterfaceInfo(DeleteIdDTO deleteIdDTO) {
        // 删除接口剩余调用次数缓存
        String key = ThreadLocalUtils.getCurrentId() + "-" + deleteIdDTO.getId();
        redisTemplate.opsForHash().delete(INTERFACE_LEFT_NUM_KEY, key);
        // 判断是否存在
        checkInterfaceExists(deleteIdDTO.getId());
        // 存在则删除
        boolean isSuccess = this.removeById(deleteIdDTO.getId());
        ThrowUtils.throwIf(!isSuccess, SERVICE_DELETE_ERROR);
        // 删除接口缓存
        deleteInterfaceCache(deleteIdDTO.getId());
        return ResponseResult.success();
    }

    /**
     * 更新接口
     *
     * @param interfaceInfoUpdateDTO
     * @return
     */
    @Override
    public ResponseResult updateInterfaceInfo(InterfaceInfoUpdateDTO interfaceInfoUpdateDTO) {
        // 更新接口信息
        InterfaceInfo interfaceInfo = BeanConvertorUtils.copyBean(interfaceInfoUpdateDTO, InterfaceInfo.class);
        String requestParamsRemarkJson = requestParamsRemarkVOToJSON(interfaceInfoUpdateDTO.getRequestParamsRemark());
        interfaceInfo.setRequestParamsRemark(requestParamsRemarkJson);
        String responseParamsRemarkJson = responseParamsRemarkVOToJSOn(interfaceInfoUpdateDTO.getResponseParamsRemark());
        interfaceInfo.setResponseParamsRemark(responseParamsRemarkJson);
        boolean isSuccess = this.updateById(interfaceInfo);
        ThrowUtils.throwIf(!isSuccess, SERVICE_UPDATE_ERROR);
        // 删除接口信息缓存
        deleteInterfaceCache(interfaceInfo.getId());
        return ResponseResult.success();
    }

    /**
     * 根据接口id查询脱敏后的接口信息
     *
     * @param queryIdDTO
     * @return
     */
    @Override
    public ResponseResult<InterfaceInfoVO> getInterfaceInfoVOById(QueryIdDTO queryIdDTO) {
        // 查询接口信息
        InterfaceInfo interfaceInfo = this.checkInterfaceExists(queryIdDTO.getId());
        InterfaceInfoVO interfaceInfoVO = BeanConvertorUtils.copyBean(interfaceInfo, InterfaceInfoVO.class);
        // 查询接口对应用户信息
        User user = userService.getById(ThreadLocalUtils.getCurrentId());

        interfaceInfoVO.setUser(BeanConvertorUtils.copyBean(user, UserVO.class));
        // 封装请求参数说明 和 响应参数说明
        if (StringUtils.isNotBlank(interfaceInfo.getResponseParamsRemark())) {
            List<RequestParamsRemarkVO> requestParamsRemarkVOList = JSONUtil.toList(
                    JSONUtil.parseArray(interfaceInfo.getRequestParamsRemark()), RequestParamsRemarkVO.class);
            interfaceInfoVO.setRequestParamsRemark(requestParamsRemarkVOList);
        }
        if (StringUtils.isNotBlank(interfaceInfo.getResponseParamsRemark())) {
            List<ResponseParamsRemarkVO> responseParamsRemarkVOList = JSONUtil.toList(
                    JSONUtil.parseArray(interfaceInfo.getResponseParamsRemark()), ResponseParamsRemarkVO.class);
            interfaceInfoVO.setResponseParamsRemark(responseParamsRemarkVOList);
        }
        return ResponseResult.success(interfaceInfoVO);
    }

    /**
     * 分页查询所有脱敏后的接口信息
     *
     * @param interfaceInfoQueryDTO
     * @return
     */
    @Override
    public ResponseResult<Page<InterfaceInfoVO>> listInterfaceInfoVOByPage(InterfaceInfoQueryDTO interfaceInfoQueryDTO) {
        // 查询所有接口
        List<InterfaceInfo> interfaceInfoList = this.list(getQueryWrapper(interfaceInfoQueryDTO));
        // 封装VO
        List<InterfaceInfoVO> interfaceInfoVOList = interfaceInfoListToInterfaceInfoVOList(interfaceInfoList);
        // 封装Page对象
        Page<InterfaceInfoVO> page = new Page<>(interfaceInfoQueryDTO.getCurrentPage(),
                interfaceInfoQueryDTO.getPageSize(), interfaceInfoVOList.size());
        page.setRecords(interfaceInfoVOList);
        return ResponseResult.success(page);
    }

    /**
     * 分页查询自身脱敏后的接口信息
     *
     * @param interfaceInfoQueryDTO
     * @return
     */
    @Override
    public ResponseResult<Page<InterfaceInfoVO>> listInterfaceInfoVOByUserIdPage(InterfaceInfoQueryDTO interfaceInfoQueryDTO) {
        // 查询出属于自己的接口
        Long userId = ThreadLocalUtils.getCurrentId();
        List<UserInterfaceInfo> userInterfaceInfoList = userInterfaceInfoService.list(new LambdaQueryWrapper<UserInterfaceInfo>()
                .eq(UserInterfaceInfo::getUserId, userId));
        if (CollectionUtils.isEmpty(userInterfaceInfoList)) {
            // 当前用户没有拥有任何接口，直接返回
            return ResponseResult.success();
        }
        List<Long> interfaceInfoIds = userInterfaceInfoList.stream()
                .map(UserInterfaceInfo::getInterfaceInfoId)
                .distinct()
                .collect(Collectors.toList());
        // 分页查询所有接口
        Long currentPage = interfaceInfoQueryDTO.getCurrentPage();
        Long pageSize = interfaceInfoQueryDTO.getPageSize();
        Page<InterfaceInfo> page = new Page<>(currentPage, pageSize);
        this.page(page, getQueryWrapper(interfaceInfoQueryDTO).in(InterfaceInfo::getId, interfaceInfoIds));
        // 封装Page
        List<InterfaceInfoVO> interfaceInfoVOList = interfaceInfoListToInterfaceInfoVOList(page.getRecords());
        Page<InterfaceInfoVO> interfaceInfoVOPage = new Page<>(page.getCurrent(),
                page.getPages(), page.getTotal());
        interfaceInfoVOPage.setRecords(interfaceInfoVOList);
        return ResponseResult.success(interfaceInfoVOPage);
    }

    /**
     * 查询条件
     */
    private LambdaQueryWrapper<InterfaceInfo> getQueryWrapper(InterfaceInfoQueryDTO interfaceInfoQueryDTO) {
        // 获取查询内容
        Long id = interfaceInfoQueryDTO.getId();
        Long userId = interfaceInfoQueryDTO.getUserId();
        String method = interfaceInfoQueryDTO.getMethod();
        Integer status = interfaceInfoQueryDTO.getStatus();
        String name = interfaceInfoQueryDTO.getName();
        String description = interfaceInfoQueryDTO.getDescription();
        String searchText = interfaceInfoQueryDTO.getSearchText();
        String sortField = interfaceInfoQueryDTO.getSortField();
        String sortOrder = interfaceInfoQueryDTO.getSortOrder();
        Date createTime = interfaceInfoQueryDTO.getCreateTime();
        // 构造查询条件
        LambdaQueryWrapper<InterfaceInfo> lambdaQueryWrapper = new LambdaQueryWrapperUtils<InterfaceInfo>()
                .addSortCondition(sortField, sortOrder)
                .eq(Objects.nonNull(id), InterfaceInfo::getId, id)
                .eq(Objects.nonNull(userId), InterfaceInfo::getUserId, userId)
                .eq(StringUtils.isNotBlank(method), InterfaceInfo::getMethod, method)
                .eq(Objects.nonNull(status), InterfaceInfo::getStatus, status)
                .eq(Objects.nonNull(createTime), InterfaceInfo::getCreateTime, createTime)
                .like(StringUtils.isNotBlank(name), InterfaceInfo::getName, name)
                .like(StringUtils.isNotBlank(description), InterfaceInfo::getDescription, description);
        if (StringUtils.isNotBlank(searchText)) {
            // 如果存在搜索词，则需要模糊查询接口名和描述信息
            lambdaQueryWrapper.like(InterfaceInfo::getName, searchText)
                    .or()
                    .like(InterfaceInfo::getDescription, searchText);
        }
        // 构造查询条件
        return lambdaQueryWrapper;
    }

    /**
     * 上线接口
     *
     * @param interfaceInfoInvokeDTO
     * @return
     */
    @Override
    public ResponseResult onlineInterfaceInfo(InterfaceInfoInvokeDTO interfaceInfoInvokeDTO) {
        // 判断接口是否存在
        Long id = interfaceInfoInvokeDTO.getId();
        InterfaceInfo oldInterfaceInfo = checkInterfaceExists(id);
        // 接口存在，则判断是否可以调用
        checkInterfaceInvoke(interfaceInfoInvokeDTO, oldInterfaceInfo);
        // 修改接口状态为上线状态
        InterfaceInfo interfaceInfo = new InterfaceInfo();
        interfaceInfo.setId(id);
        interfaceInfo.setStatus(InterfaceInfoEnum.ONLINE.getValue());
        boolean isSuccess = this.updateById(interfaceInfo);
        ThrowUtils.throwIf(!isSuccess, SERVICE_UPDATE_ERROR);
        // 删除接口缓存
        deleteInterfaceCache(interfaceInfo.getId());
        return ResponseResult.success();
    }

    /**
     * 下线接口
     *
     * @param updateIdDTO
     * @return
     */
    @Override
    public ResponseResult offlineInterfaceInfo(UpdateIdDTO updateIdDTO) {
        // 判断接口是否存在
        Long id = updateIdDTO.getId();
        checkInterfaceExists(id);
        // 接口存在，则直接下线接口
        InterfaceInfo interfaceInfo = new InterfaceInfo();
        interfaceInfo.setId(id);
        interfaceInfo.setStatus(InterfaceInfoEnum.OFFLINE.getValue());
        boolean isSuccess = this.updateById(interfaceInfo);
        ThrowUtils.throwIf(!isSuccess, SERVICE_UPDATE_ERROR);
        // 删除接口缓存
        deleteInterfaceCache(interfaceInfo.getId());
        return ResponseResult.success();
    }

    /**
     * 开通接口
     *
     * @param updateIdDTO
     * @return
     */
    @Override
    public ResponseResult buyInterfaceInfo(UpdateIdDTO updateIdDTO) {
        Long interfaceId = updateIdDTO.getId();
        Long userId = ThreadLocalUtils.getCurrentId();
        // 添加接口调用次数缓存
        String key = userId + "-" + interfaceId;
        redisTemplate.opsForHash().put(INTERFACE_LEFT_NUM_KEY, key, DEFAULT_LEFTNUM.getValue());
        // 判断接口是否已开通
        checkInterfaceBuy(interfaceId, userId);
        // 判断接口是否存在
        InterfaceInfo interfaceInfo = checkInterfaceExists(updateIdDTO.getId());
        // 判断接口是否开启
        ThrowUtils.throwIf(interfaceInfo.getStatus().equals(InterfaceInfoEnum.OFFLINE.getValue()),
                INTERFACE_DISABLE);
        // 接口存在，则是开通接口
        UserInterfaceInfo userInterfaceInfo = new UserInterfaceInfo();
        userInterfaceInfo.setUserId(ThreadLocalUtils.getCurrentId());
        userInterfaceInfo.setInterfaceInfoId(updateIdDTO.getId());
        userInterfaceInfo.setLeftNum(DEFAULT_LEFTNUM.getValue());
        userInterfaceInfoService.save(userInterfaceInfo);
        // 删除接口缓存
        deleteInterfaceCache(interfaceInfo.getId());
        return ResponseResult.success();
    }

    /**
     * 调用接口
     *
     * @param interfaceInfoInvokeDTO
     * @return
     */
    @Override
    public ResponseResult<String> invokeInterfaceInfo(InterfaceInfoInvokeDTO interfaceInfoInvokeDTO) {
        // 判断接口是否存在
        InterfaceInfo interfaceInfo = checkInterfaceExists(interfaceInfoInvokeDTO.getId());
        // 判断接口是否开启
        ThrowUtils.throwIf(interfaceInfo.getStatus().equals(InterfaceInfoEnum.OFFLINE.getValue()),
                INTERFACE_DISABLE);
        // 判断段接口能否调用
        String result = checkInterfaceInvoke(interfaceInfoInvokeDTO, interfaceInfo);
        return ResponseResult.success(result);
    }

    /**
     * 检查接口是否存在
     */
    private InterfaceInfo checkInterfaceExists(Long id) {
        // 根据id查询接口信息（采用缓存空对象的方式成功解决缓存穿透问题）
//        InterfaceInfo interfaceInfo = this.getById(id);
        InterfaceInfo interfaceInfo = cacheClient.handleCachePenetrationByCacheEmptyObject(
                INTERFACE_KEY, id, InterfaceInfo.class,
                this::getById, INTERFACE_CACHE_TTL, TimeUnit.SECONDS);
        ThrowUtils.throwIf(Objects.isNull(interfaceInfo), INTERFACE_NOT_EXISTS);
        return interfaceInfo;
    }

    /**
     * 检查接口能否正常调用
     */
    private String checkInterfaceInvoke(InterfaceInfoInvokeDTO interfaceInfoInvokeDTO, InterfaceInfo interfaceInfo) {
        ApiClient apiClient = getApiClient();
        String responseResult = null;
        String requestParams = interfaceInfoInvokeDTO.getRequestParams();
        String url = interfaceInfo.getUrl();
        String method = interfaceInfo.getMethod();
        // 使用 api-feign-sdk 项目封装的 apiClient 向 api-hub-gateway 发送请求
        responseResult = apiClient.invokeInterface(requestParams, url, method);
        // 判断接口是否正常，如果返回信息为空则说明接口不正常
        ThrowUtils.throwIf(StringUtils.isBlank(responseResult), SYSTEM_ERROR);
        return responseResult;
    }

    /**
     * 获取ApiClient对象
     *
     * @return
     */
    @NotNull
    private ApiClient getApiClient() {
        // 获取当前用户对应的accessKey、secretKey
        User user = userService.getById(ThreadLocalUtils.getCurrentId());
        String accessKey = user.getAccessKey();
        String secretKey = user.getSecretKey();
        // 调用接口
        ApiClient apiClient = new ApiClient(accessKey, secretKey);
        return apiClient;
    }

    /**
     * 检查接口是否已购买
     */
    private void checkInterfaceBuy(Long interfaceId, Long userId) {
        UserInterfaceInfo userInterfaceInfo = userInterfaceInfoService.getOne(new LambdaQueryWrapper<UserInterfaceInfo>()
                .eq(UserInterfaceInfo::getInterfaceInfoId, interfaceId)
                .eq(UserInterfaceInfo::getUserId, userId));
        ThrowUtils.throwIf(Objects.nonNull(userInterfaceInfo), INTERFACE_ALREADY_BUY);
    }

    /**
     * 将 List<interfaceInfo> 对象转成 List<interfaceInfoVO>
     */
    private List<InterfaceInfoVO> interfaceInfoListToInterfaceInfoVOList(List<InterfaceInfo> interfaceInfoList) {
        // 查询当前用户所拥有的接口
        Long userId = ThreadLocalUtils.getCurrentId();
        Map<Long, UserInterfaceInfo> userInterfaceInfoMap = userInterfaceInfoService.list(new LambdaQueryWrapper<UserInterfaceInfo>()
                        .eq(UserInterfaceInfo::getUserId, userId))
                .stream().collect(Collectors.toMap(UserInterfaceInfo::getInterfaceInfoId,
                        userInterfaceInfo -> userInterfaceInfo));
        Set<Long> interfaceInfoIds = userInterfaceInfoMap.keySet();
        // 将 List<interfaceInfo> 对象转成 List<interfaceInfoVO>
        return interfaceInfoList.stream().map(interfaceInfo -> {
            InterfaceInfoVO interfaceInfoVO = BeanConvertorUtils.copyBean(interfaceInfo, InterfaceInfoVO.class);
            // 封装请求参数和响应参数
            List<RequestParamsRemarkVO> requestParamsRemarkVOList = JSONUtil.toList(
                    JSONUtil.parseArray(interfaceInfo.getRequestParamsRemark()),
                    RequestParamsRemarkVO.class);
            List<ResponseParamsRemarkVO> responseParamsRemarkVOList = JSONUtil.toList(
                    JSONUtil.parseArray(interfaceInfo.getResponseParamsRemark()),
                    ResponseParamsRemarkVO.class);
            interfaceInfoVO.setRequestParamsRemark(requestParamsRemarkVOList);
            interfaceInfoVO.setResponseParamsRemark(responseParamsRemarkVOList);
            if (interfaceInfoIds.contains(interfaceInfoVO.getId())) {
                // 封装leftNum和totalNum
                UserInterfaceInfo userInterfaceInfo = userInterfaceInfoMap.get(interfaceInfoVO.getId());
                interfaceInfoVO.setLeftNum(userInterfaceInfo.getLeftNum());
                interfaceInfoVO.setTotalNum(userInterfaceInfo.getTotalNum());
                // 封装isOwnerByCurrentUser属性
                interfaceInfoVO.setIsOwnerByCurrentUser(true);
            }
            return interfaceInfoVO;
        }).collect(Collectors.toList());


//        return BeanConvertorUtils
//                .copyBeanList(interfaceInfoList, InterfaceInfoVO.class)
//                .stream().map(interfaceInfoVO -> {
//                    // 封装请求参数和响应参数
//                    List<RequestParamsRemarkVO> requestParamsRemarkVOList = JSONUtil.toList(
//                            JSONUtil.parseArray(interfaceInfoVO.getRequestParamsRemark()),
//                            RequestParamsRemarkVO.class);
//                    List<ResponseParamsRemarkVO> responseParamsRemarkVOList = JSONUtil.toList(
//                            JSONUtil.parseArray(interfaceInfoVO.getResponseParamsRemark()),
//                            ResponseParamsRemarkVO.class);
//                    interfaceInfoVO.setRequestParamsRemark(requestParamsRemarkVOList);
//                    interfaceInfoVO.setResponseParamsRemark(responseParamsRemarkVOList);
//                    if (interfaceInfoIds.contains(interfaceInfoVO.getId())) {
//                        // 封装leftNum和totalNum
//                        UserInterfaceInfo userInterfaceInfo = userInterfaceInfoMap.get(interfaceInfoVO.getId());
//                        interfaceInfoVO.setLeftNum(userInterfaceInfo.getLeftNum());
//                        interfaceInfoVO.setTotalNum(userInterfaceInfo.getTotalNum());
//                        // 封装isOwnerByCurrentUser属性
//                        interfaceInfoVO.setIsOwnerByCurrentUser(true);
//                    }
//                    return interfaceInfoVO;
//                }).collect(Collectors.toList());
    }

    /**
     * 更新、删除接口时删除缓存
     */
    private boolean deleteInterfaceCache(Long interfaceId) {
        // 删除接口信息
        Boolean isSuccess = redisTemplate.delete(INTERFACE_KEY + interfaceId);
        return BooleanUtil.isTrue(isSuccess);
    }

    /**
     * 将 List<RequestParamsRemarkVO> 转成 json 格式的字符串
     */
    private String requestParamsRemarkVOToJSON(List<RequestParamsRemarkVO> requestParamsRemarkVOList){
        // 假设将要转换的对象列表存储在 requestParamsRemarkVOList 中
        List<Map<String, Object>> resultList = new ArrayList<>();
        for (RequestParamsRemarkVO vo : requestParamsRemarkVOList) {
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("id", vo.getId());
            resultMap.put("name", vo.getName());
            resultMap.put("isRequired", vo.getIsRequired());
            resultMap.put("type", vo.getType());
            resultMap.put("remark", vo.getRemark());
            resultList.add(resultMap);
        }
        // 将转换结果转换成 JSON 格式
        String jsonResult = new Gson().toJson(resultList);
        return jsonResult;
    }

    /**
     * 将 List<ResponseParamsRemarkVO> 转成 json 格式的字符串
     */
    private String responseParamsRemarkVOToJSOn(List<ResponseParamsRemarkVO> responseParamsRemarkVOList){
        // 假设将要转换的对象列表存储在 requestParamsRemarkVOList 中
        List<Map<String, Object>> resultList = new ArrayList<>();
        for (ResponseParamsRemarkVO vo : responseParamsRemarkVOList) {
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("id", vo.getId());
            resultMap.put("name", vo.getName());
            resultMap.put("type", vo.getType());
            resultMap.put("remark", vo.getRemark());
            resultList.add(resultMap);
        }
        // 将转换结果转换成 JSON 格式
        String jsonResult = new Gson().toJson(resultList);
        return jsonResult;
    }
}




